package com.situ.day5;

import java.util.Scanner;

import com.situ.day3.Student;

public class StudentManager {

	public static void main(String[] args) {
		Scanner scanner = new Scanner(System.in);
		System.out.println("请输入学生个数：");
		int count = scanner.nextInt();
		Student[] students = new Student[count];
		//输入学生信息到学生数组里面
		inputStudents(students);
		//打印数组中的学生信息
		printStudents(students);
		//根据条件来搜索
		searchByCondition(students);
	}

	private static void searchByCondition(Student[] students) {
		Scanner scanner = new Scanner(System.in);
		// for (;;) {}
		while (true) {
			System.out.println("--------------");
			System.out.println("根据id查找：	1");
			System.out.println("根据名字查找：	2");
			System.out.println("根据年龄查找：	3");
			System.out.println("根据班级查找：	4");
			System.out.println("推出程序：		0");
			System.out.println("--------------");
			
			int type = scanner.nextInt();
			if (type == 0) {
				System.out.println("退出程序");
				return;
			}
			
			switch (type) {
			case 1://根据id查找
				System.out.println("id");
				System.out.println("请输入要查找的id：");
				// 遍历Student[]，找到id相同的学生，就打印这个学生的信息。
				// 如果遍历完所有学生都没有查找到，就打印“没有该学生信息”
				int id = scanner.nextInt();
				searchById(students, id);
				break;
			case 2:
				System.out.println("name");
				System.out.println("请输入学生名字：");
				String name = scanner.next();
				boolean isSearchByNameFind = false;
				for (int i = 0; i < students.length; i++) {
					Student student = students[i];
					if (name.equals(student.getName())) {
						System.out.println(student);
						isSearchByNameFind = true;
					}		
				}
				if (isSearchByNameFind == false) {
					System.out.println("没有该学生信息");
				}
				
				break;
			case 3:
				System.out.println("age");
				System.out.println("请输入学生年龄：");
				int age = scanner.nextInt();
				boolean isSearchByAgeFind = false;
				for (int i = 0; i < students.length; i++) {
					Student student = students[i];
					if (age == student.getAge()) {
						System.out.println(student);
						isSearchByAgeFind = true;
					}		
				}
				if (isSearchByAgeFind == false) {
					System.out.println("没有该学生信息");
				}				
				break;
			case 4:
				System.out.println("className");
				System.out.println("请输入学生班级：");
				String className = scanner.next();
				boolean isSearchByClassNameFind = false;
				for (int i = 0; i < students.length; i++) {
					Student student = students[i];
					if (className.equals(student.getClassName())) {
						System.out.println(student);
						isSearchByClassNameFind = true;
					}		
				}
				if (isSearchByClassNameFind == false) {
					System.out.println("没有该学生信息");
				}		
				break;
			default:
				break;
			}
		}
	}

	/**
	 * 根据id查找学生信息
	 * @param students
	 * @param id
	 */
	private static void searchById(Student[] students, int id) {
		boolean isSearchByIdFind = false;//初始认为根据id查找没有找到
		for (int i = 0; i < students.length; i++) {
			Student student = students[i];
			if (id == student.getId()) {
				System.out.println(student);
				isSearchByIdFind = true;
			}
		}
		if (isSearchByIdFind == false) {
			System.out.println("没有该学生信息");
		}
	}

	/**
	 * 打印所有学生信息
	 * @param students
	 */
	private static void printStudents(Student[] students) {
		System.out.println("学生列表信息：");
		for (int i = 0; i < students.length; i++) {
			Student student = students[i];
			System.out.println(student);
		}
	}

	/**
	 * 输入学生信息
	 * @param students
	 */
	private static void inputStudents(Student[] students) {
		Scanner scanner = new Scanner(System.in);
		for (int i = 0; i < students.length; i++) {
			System.out.println("请输入学生的id：");
			int id = scanner.nextInt();
			System.out.println("请输入学生的名字：");
			String name = scanner.next();
			System.out.println("请输入学生的年龄：");
			int age = scanner.nextInt();
			System.out.println("请输入学生班级：");
			//The value of the local variable className is not used
			String className = scanner.next();
			
			Student student = new Student(id, name, age, className);
			students[i] = student;
		}
	}
	
}
